<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <!-- 列表 -->
    <div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :immediate-check="false"
        @load="onLoad"
      >
        <van-cell
          v-for="item in searchResultList"
          :key="item.art_id"
          @click="details(item.art_id)"
        >
          <!-- 标题区域的插槽 -->
          <template #title>
            <div class="title-box">
              <!-- 标题 -->
              <span>{{ item.title }}</span>
              <img
                class="thumb"
                v-if="item.cover.type === 1"
                :src="item.cover.images[0]"
              />
            </div>
            <div class="thumb-box" v-if="item.cover.type > 1">
              <img
                class="thumb"
                v-for="(imgUrl, index) in item.cover.images"
                :key="index"
                :src="imgUrl"
              />
            </div>
          </template>
          <!-- label 区域的插槽 -->
          <template #label>
            <div class="label-box">
              <div>
                <span>{{ item.aut_name }}</span>
                <span>{{ item.comm_count }}评论</span>
                <span>{{ timeFormat(item.pubdate) }}</span>
              </div>
            </div>
          </template>
        </van-cell>
      </van-list>
    </div>
  </div>
</template>

<script>
import { reqSearchResult, reqArticleDetail } from "@/api";
import { Notify } from "vant";
import { timeAgo } from "@/utils/day.js";
export default {
  name: "SearchResult",
  data() {
    return {
      page: 1,
      searchResultList: [],
      loading: false,
      finished: false,
    };
  },
  async created() {
    try {
      const res = await reqSearchResult({
        page: this.page,
        q: this.$route.params.kw,
      });
      if (res.status == 200) {
        this.searchResultList = res.data.data.results;
      }
    } catch (error) {
      Notify({ type: "primary", message: "请求失败" });
    }
  },
  methods: {
    timeFormat: timeAgo,
    async onLoad() {
      if (this.searchResultList.length > 0) {
        this.page++;
        const res = await reqSearchResult({
          page: this.page,
          q: this.$route.params.kw,
        });
        if (res.data.data.results.length == 0) {
          this.finished = true;
          return;
        }
        if (res.status == 200) {
          this.searchResultList = [
            ...this.searchResultList,
            ...res.data.data.results,
          ];
        }
        this.loading = false;
      }
    },
    details(detail_id) {
      this.$router.push({
        path: `/detail?art_id=${detail_id}`,
      });
    },
  },
};
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}
/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
</style>

<style lang="less">
// 图标颜色
.van-nav-bar .van-icon {
  color: antiquewhite;
}
</style>
